# open
# #open(file, mode='r', buffering=-1, encoding=utf8, errors=None, newline=None, closefd=True, opener=None)
# file  指定打开的文件
# mode  指定文件打开方式
# encoding  指定文件编码
# errors  发生错误时的处理方式
# buffering 指定缓存方式

##encoding#########
# >>> fp = open("utf.txt","w+",encoding="utf-8")
# >>> fp.write("hello world")
# 11
# >>> fp.close()
# >>> fp = open("utf.txt","w+",encoding="utf-8")
# >>> fp.wirte("你好")
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# AttributeError: '_io.TextIOWrapper' object has no attribute 'wirte'
# >>> fp.write("你好")
# 2
# >>> fp.close()
# >>> fp.write("ok")
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# ValueError: I/O operation on closed file.
# >>> fp = open("gbk.txt","w+",encoding="gbk")
# >>> fp.write("你好")
# 2
# >>> fp.close()
# >>> fp = open("gbk.txt","w+",encoding="gbk")
# >>> fp.write("这是gbk")
# 5
# >>> fp.close()
# >>> exit()

#linux命令 可以通过file 查看文件编码
# [root@localhost openpython]# file gbk.txt
# gbk.txt: ISO-8859 text, with no line terminators

# >>> fp = open("utf.txt")  #不指定编码的话，会以操作系统的默认编码方式读写
# >>> fp.read()
# '你好'
# >>> fp2 = open("gbk.txt",encoding="gbk")
# >>> fp2.read()
# '这是gbk'

########
# >>> fp = open("utf.txt")
# >>> fp.read()
# '你好'
# >>> fp = open("win-utf-8.txt")
# >>> fp.read()
# 'windows的utf-8文本'
# >>> fp = open("win-utf-8.txt",encodig="utf-8-sig")
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: 'encodig' is an invalid keyword argument for this function
#     >>> fp = open("win-utf-8.txt",encoding="utf-8-sig")
# >>> fp.read()
# 'windows的utf-8文本'

############python中 文件编码检测模块
# >>> import chardet
# >>> fp = open("utf.txt","rb")
# >>> chardet.detect(f.read())
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# NameError: name 'f' is not defined
# >>> chardet.detect(fp.read())
# {'encoding': 'utf-8', 'confidence': 0.7525, 'language': ''}
# >>> fp = open("win-utf-8.txt","rb")
# >>> chardet.detect(fp.read())
# {'encoding': 'utf-8', 'confidence': 0.87625, 'language': ''}


###文件的打开方式#########
#默认(只读)

# >>> fp = open("utf.txt")
# >>> fp.read()
# '你好'
# >>> fp.write("hello")
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# io.UnsupportedOperation: not writable
# >>> fp = open("utf.txt","r")
# >>> fp.read()
# '你好'
# >>> fp.write("hello")
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# io.UnsupportedOperation: not writable
# >>> fp = open("utf.txt","w")
# >>> fp.read()
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# io.UnsupportedOperation: not readable
# >>> fp.write("hello")
# 5
# >>> fp.close()
# >>> fp = open("utf.txt","w+")
# >>> fp.read()
# ''
# >>> fp.write("haha")
# 4
# >>> fp.close()
# >>> fp = open("utf.txt","rb")
# >>> fp.read()
# b'haha'


########with语句关键字 管理open########
# with open("test1.txt","r") as f:
#     for i in f:
#         print(i)
#     # print(f.read())

###########文件读取
#read
#readline
#readlines
###注意光标的位置
# >>> fp = open("test1.txt","r+")
# >>> fp.read()
# 'a\nb\nc\nd\ne\nf\ng\n'
# >>> fp.read()
# ''
# >>> fp = open("test1.txt","r+")
# >>> fp.readline()
# 'a\n'
# >>> fp.readline()
# 'b\n'
# >>> fp.readline()
# 'c\n'
# >>> fp.readline()
# 'd\n'
# >>> fp.readline()
# 'e\n'
# >>> fp.readline()
# 'f\n'
# >>> fp.readline()
# 'g\n'
# >>> fp.readline()
# ''
# >>> fp.readline()
# ''
# >>> fp = open("test1.txt","r+")
# >>> fp.readlines()
# ['a\n', 'b\n', 'c\n', 'd\n', 'e\n', 'f\n', 'g\n']
# >>> fp = open("test1.txt","r+")
# >>> fp.read(10)         #根据指定字符数读取
# 'a xixi\nb h'
# >>> fp.read(10)
# 'aha\nc hell'

# #########光标设置#########
# >>> fp.readline()
# 'b\n'
# >>> fp.readline()
# 'c\n'
# 'aha\nc hell'
# >>> fp.tell()
# 20
# >>> fp.seek(0)
# 0
# >>> fp.tell()
# 0
# >>> fp.read(10)
# 'a xixi\nb h'
# >>> fp.seek(16)
# 16
# >>> fp.tell()
# 16
# >>> fp.read(10)
# 'hello\nd wo'
#>>> fp = open("test1.txt","rb")
# >>> fp.read(10)
# b'a xixi\nb h'
# >>> fp.tell()
# 10
# >>> fp.seek(10)                                                                                                                                             >>
# > fp.seek(10,1)
# 20
# >>> fp.read(10)
# b'o\nd world\n'
# >>> fp.seek(10,2)
# 63
# >>> fp.read()
# b''
# >>> fp.seek(-10,2)
# 43
# >>> fp.read()
# b'nchuang\ng\n'

##########练习
# with open("test1.txt","rb") as f:
#     # for i in range(5):
#     #     print(f.readline())
#     # f.seek(0)
#     # str1 = f.read()
#     # print(str1)
#     f.seek(-12,2)
#     print(f.read().decode("utf-8"))

#####文件写入
# >>> fp = open("test1.txt","w+")>>> fp.write("hello1")                                                                                                                                        6
# >>> fp.flush()
# >>> fp.write("\nhello2")7
# >>> fp.flush()
# >>> fp = open("test1.txt","w+",buffering=1)
# >>> fp.write("hello")
# 5
# >>> fp.write("\n")
# 1
# >>> fp = open("test1.txt","w+",buffering=2)
# >>> fp.write("a"*4096)
# 4096
# >>> fp.write("a"*4095)
# 4095
# >>> fp.write("a")
# 1
# >>> fp.write("a")
# 1